package com.healthcloud.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.*;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.SysUserService;
import com.healthcloud.service.WHospitalService;
import com.healthcloud.utils.CommonMethod;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.service.HlDoctorService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 医生信息表
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2017-08-28 16:38:36
 */
@ApiIgnore
@Controller
@RequestMapping("hldoctor")
public class HlDoctorController {

	@Autowired
	private HlDoctorService hlDoctorService;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private HlHospitalService hlHospitalService;

	@Autowired
	private CommonMethod commonMethod;

	@Autowired
	private WHospitalService wHospitalService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("hldoctor:list")
	public String list(@RequestParam Map<String, Object> params){
		return "bus/hldoctor/hldoctormgr";
	}
	
	@RequiresPermissions("hldoctor:list")
	@RequestMapping(value = "/searchHlDoctor")
	@ResponseBody
    public MsgVo  searchHlDoctor(HlDoctorEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        bean.setIds(hlHospitalService.getChildeIds());
        List<HlDoctorEntity> list = hlDoctorService.queryList(bean, pageParam);
        for(HlDoctorEntity doctorEntity : list){

            doctorEntity.setText(doctorEntity.getDrName());
        }
        int count = hlDoctorService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
	
	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("hldoctor:save")
    public String toSave(Model model) {
        return "bus/hldoctor/addOrUpdatehldoctor";
    }
    
	/**
	 * 保存
	 */
    @SysLog("保存HlDoctor")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(FormVo form, String[] hospId_officeCode, String hospitalCode, Long hospId) {

        SysUserEntity user = form.getUser();

        HlDoctorEntity doctor = form.getDoctor();

        if(user.getMobile()!=null){
            user.setMobile(user.getMobile().toLowerCase());
        }

        if(user.getUsername()!=null){
            user.setUsername(user.getUsername().toLowerCase());
        }

        Map<Long,List<Long>> map = new HashMap<>();
        if(hospId_officeCode!=null){
            for (String hioc : hospId_officeCode) {
                String[] hiocArr = hioc.split("_");
                if(hiocArr==null || hiocArr.length!=2){
                    return new ApiRes(false,null,null,null);
                }
                Long hi = Long.valueOf(hiocArr[0]);
                Long oc = Long.valueOf(hiocArr[1]);
                if(map.containsKey(hi)){
                    map.get(hi).add(oc);
                }else{
                    List<Long> ocList = new ArrayList<>();
                    ocList.add(oc);
                    map.put(hi,ocList);
                }
            }
        }
        hlDoctorService.register(user, doctor, map);
        hlHospitalService.bindWHospital(hospId, hospitalCode);

        return ApiRes.SUCCESS;
    }
	
	/**
	 * 修改页9
	 */	
	@RequestMapping("/toupdate")
    @RequiresPermissions("hldoctor:update")
    public String toUpdate(@Param("id") Long id, Model model) {
        //医生基本信息
	    HlDoctorEntity doctor = hlDoctorService.queryObject(id);
        model.addAttribute("doctor", doctor);

        //用户数据
        SysUserEntity user = sysUserService.queryObjectByid(doctor.getUserId());
        model.addAttribute("user", user);

        //医院，科室数据
        HospDrAsso asso = new HospDrAsso();
        asso.setDrId(doctor.getId());
        List<HospDrAsso> assos = hlDoctorService.getHospDrAsso(asso);
        model.addAttribute("assos", assos);

        //医院
        if (assos!=null && assos.size()>0){
            HlHospitalEntity hospital = hlHospitalService.queryObject(assos.get(0).getHospId());
            model.addAttribute("hospital", hospital);

            //报告单医院
            if (StringUtils.isNotEmpty(hospital.getHospitalCode())){
                WHospitalEntity wHospital = wHospitalService.getByHospitalCode(hospital.getHospitalCode());
                if (wHospital!=null) {
                    model.addAttribute("wHospital", wHospital);
                }
            }
        }

        return "bus/hldoctor/addOrUpdatehldoctor";

    }
	
    /**
     * 修改
     */
    @SysLog("修改HlDoctor")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(FormVo form, String[] hospId_officeCode, String hospitalCode, Long hospId) {

        SysUserEntity user = form.getUser();

        HlDoctorEntity doctor = form.getDoctor();

        if(user.getMobile()!=null){
            user.setMobile(user.getMobile().toLowerCase());
        }

        if(user.getUsername()!=null){
            user.setUsername(user.getUsername().toLowerCase());
        }
        Map<Long,List<Long>> map = new HashMap<>();
        if(hospId_officeCode!=null){
            for (String hioc : hospId_officeCode) {
                String[] hiocArr = hioc.split("_");
                if(hiocArr==null || hiocArr.length!=2){
                    return new ApiRes(false,null,null,null);
                }
                Long hi = Long.valueOf(hiocArr[0]);
                Long oc = Long.valueOf(hiocArr[1]);
                if(map.containsKey(hi)){
                    map.get(hi).add(oc);
                }else{
                    List<Long> ocList = new ArrayList<>();
                    ocList.add(oc);
                    map.put(hi,ocList);
                }
            }
        }
        hlDoctorService.update(user, doctor, map);
        hlHospitalService.bindWHospital(hospId, hospitalCode);
        return new ApiRes(true,null,null,null);
    }

	
    /**
     * 删除
     */
    @SysLog("删除HlDoctor")
    @RequestMapping("/delete")
    @RequiresPermissions("hldoctor:delete")
    @ResponseBody
    public ApiRes delete(Long[] ids) {
        hlDoctorService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @RequestMapping(value = "/selectDoctor")
    @ResponseBody
    public ApiRes searchHlDoctor(HlDoctorEntity bean, @ModelAttribute("pageParam")PageParam pageParam){
        HlHospitalEntity hospitalBean = commonMethod.getHospitalBean();
        if(hospitalBean != null){
            bean.setIds(String.valueOf(hospitalBean.getId()));
        }
        List<HlDoctorEntity> list = hlDoctorService.queryList(bean, pageParam);
        for(HlDoctorEntity doctorEntity : list){
            doctorEntity.setText(doctorEntity.getDrName());
        }
        int count = hlDoctorService.queryTotal(bean);
        return new ApiRes(true,list,null,String.valueOf(count));
    }

    /**
     * 下拉框查询
     */
    @RequestMapping("/select")
    @ResponseBody
    public ApiRes selectHospName(HlHospitalEntity bean, @ModelAttribute("pageParam")PageParam pageParam){

        List<HlHospitalEntity> list = hlHospitalService.queryList(bean, pageParam);
        int total = hlHospitalService.queryTotal(bean);
        return new ApiRes(true,list,null,String.valueOf(total));
    }

}
